import com.google.jplurk.*
import org.cyberneko.html.parsers.*

words = ['channel', '頻道']

words.each {
	word ->
	
	if (plurk.content.toLowerCase().endsWith(word)) {
		println "match ${word}"
		
		ch_name = plurk.content.substring(0, plurk.content.length()-word.length()).trim()
		println "查詢 ${ch_name}"
		
		def parser = new SAXParser()
		parser.setFeature('http://xml.org/sax/features/namespaces', false)
		page = new XmlParser(parser).parse('http://www.niotv.com/i_index.php?cont=now')
		
		ch_id = 0
		data = page.depthFirst().A.grep {
			it.value() &&
			it.value().text() &&
			it.value().text().toLowerCase().contains(ch_name.toLowerCase()) &&
			it.'@href'.startsWith('i_index')
		}
		println data
		data.each {
			def m = (it.'@href' =~ "sch_id=([0-9]+)")
			ch_id = m[0][1]
		}
		println "發現頻道編號 ${ch_id}"
		
		result = []
		data = page.depthFirst().A.grep { it.'@href' != null && it.'@href'.endsWith("ch_id=${ch_id}") }
		data.each { result << it.value().text() }
		
		matched++
		ship.response plurk.plurk_id, "${ch_name}正在播${result[0]}，接下來播${result[1]}", Qualifier.SAYS
	}
}
